<template>
	<view class="container">
		<block v-if="!isDetail">
			<view class="choose">
				<picker mode="selector" :range="schoolArr" :value="sindex" @change="shool">
					<view>{{schoolArr[sindex]}}</view>
				</picker>
				<picker mode="selector" :range="floorArr" :value="findex" @change="floor">
					<view>{{floorArr[findex]}}</view>
				</picker>
				<picker mode="selector" :range="typeArr" :value="tindex" @change="type">
					<view>{{typeArr[tindex]}}</view>
				</picker>
			</view>
			<view v-if="roomArr.length != 0" class="allRoom" v-for="(item,index) in roomArr" :key="index">
				<view class="oneRoom">
					<view class="num">{{item.roomNum}}</view>
					<view class="peopleNum">总人数：{{item.members.length}}人</view>
					<view class="info" @click="goDetail(item)">详情</view>
				</view>
			</view>
			<view class="tips" v-else>
				<view class="add">
					<span class="iconfont icon-jiahao addRoom" @click="goCreateRoom"></span>
				</view>
				<input v-model="tips" />
			</view>
		</block>
		<block v-else>
			<view class="roomList">
				<view class="showRoomNum">当前宿舍为{{activeRoomNum}}宿舍<span class="iconfont icon-shezhi"
						@click="setRoom()"></span></view>
				<view class="roomitem" v-for="(item,index) in activeMembers" :key="index">
					<view class="members">
						<view class="stname">{{item.name}}</view>
						<radio-group class="typelist" name="type" @change="stType" :data-stname="item.name">
							<label>
								<radio value="1" :checked="item.type==1?true:false"/><text class="zai">在寝</text>
							</label>
							<label>
								<radio value="2" :checked="item.type==2?true:false"/><text class="li">离寝</text>
							</label>
							<label>
								<radio value="3" :checked="item.type==3?true:false"/><text class="qing">请假</text>
							</label>
						</radio-group>
					</view>
				</view>
				<view class="btn">
					<button type="primary" @click="roomCommit">提交</button>
					<button type="default" @click="goBack">返回</button>
				</view>
			</view>
		</block>
	</view>
</template>

<script>
	import checkALL from "../../tools/checkToken/index.js";
	import doErro from "../../tools/doErro/index.js";
	import timeMachine from "../../tools/timeMachine/index.js";
	export default {
		onShow() {
			this.isDetail = false;
			let that = this;
			uni.showLoading({
				title: "数据加载中...",
				mask: true
			});
			//根据uid查询校区
			uniCloud.callFunction({
				name: "rc_room",
				data: {
					type: 'getSchoolArr',
					uid: getApp().globalData.uid
				},
				success(res) {
					if (res.result.affectedDocs == 0) {
						that.schoolArr = [];
						that.schoolArr[0] = "选择校区";
						that.sindex = 0;
						that.roomArr = [];
						uni.hideLoading();
					}
					let list = res.result.data;
					list.forEach((item, index) => {
						that.schoolArr[index + 1] = item.school;
					});
					that.schoolArr = Array.from(new Set(that.schoolArr));
					that.getRoomList();
					uni.hideLoading();
				}
			})
		},
		data() {
			return {
				hidenum: -1,
				sindex: 0,
				findex: 0,
				tindex: 0,
				schoolArr: ["选择校区"],
				floorArr: ["选择楼层", "1层", "2层", "3层", "4层", "5层", "6层", "7层", "8层", "9层", "10层"],
				typeArr: ['选择性别', '男', '女'],
				roomArr: [],
				tips: "请选择查询条件，查询宿舍",
				//以下是详情页相关属性
				isDetail: false,
				activeRoomId: "",
				activeSchool: "", //当前校区
				activeFloor: "", //当前楼层
				activeRoomNum: 0, //当前宿舍
				activeType: "", //当前性别
				activeMembers: [],
				activeUid: ""
			}
		},
		methods: {
			shool(e) {
				this.sindex = e.detail.value;
				this.getRoomList();
			},
			floor(e) {
				this.findex = e.detail.value;
				this.getRoomList();
			},
			type(e) {
				this.tindex = e.detail.value;
				//this.tindex = '1';
				this.getRoomList();
			},
			doHide(index) {
				this.roomArr[index].ishide = !this.roomArr[index].ishide;
			},
			//根据条件查询宿舍列表
			getRoomList() {
				let that = this;
				if (this.schoolArr[this.sindex] == "选择校区" &&
					this.findex == 0 &&
					this.tindex == 0) {
					this.roomArr = [];
					this.tips = "请选择查询条件，查询宿舍";
					return;
				}
				uni.showLoading({
					title: "查询中...",
					mask: true
				})
				uniCloud.callFunction({
					name: "rc_room",
					data: {
						school: this.schoolArr[this.sindex],
						floor: this.findex,
						roomtype: this.tindex,
						uid: getApp().globalData.uid
					},
					success(data) {
						//console.log(data);
						if (data.result.data.length == 0) {
							that.tips = "未查到数据，请修改查询条件"
						}
						that.roomArr = data.result.data;
						uni.hideLoading();
					},
					fail(err) {
						console.log(err);
					}
				})
			},
			//以下是详情页相关函数
			goDetail(roomDetail) {
				let that = this;
				//如果可以在count表查到结果说明已经查过了，那么列表显示时则显示查宿结果，否则不显示查宿结果只显示人名
				//console.log(roomDetail._id);
				let timeObj = timeMachine.doTime(1); //时间机器函数
				that.activeRoomId = roomDetail._id;
				that.isDetail = true;
				//预先准备提交数据
				that.activeSchool = roomDetail.school;
				that.activeFloor = roomDetail.floor;
				that.activeRoomNum = roomDetail.roomNum;
				that.activeType = roomDetail.roomtype;
				that.activeUid = roomDetail.uid;
				uniCloud.callFunction({
					name:"rc_roomdetail",
					data:{
						roomid:roomDetail._id,
						timeObj
					},
					success(res) {
						//console.log(res.result.data.length);
						if(res.result.data.length > 0){
							//说明已经查过该宿舍
							res.result.data.forEach((item,key)=>{
								that.activeMembers[key] = {
									name: item.stname,
									type: item.stType
								}
							})
						}else{
							roomDetail.members.forEach((item, key) => {
								that.activeMembers[key] = {
									name: item,
									type: ""
								}
							});
						}
					},
					fail(err) {
						console.log(err);
					}
				})
			},
			goBack() {
				this.isDetail = false;
			},
			//动态保存查宿数据函数
			stType(e) {
				this.activeMembers.forEach((item) => {
					if (item.name == e.target.dataset.stname) {
						item.type = e.detail.value;
					}
				})
			},
			roomCommit() {
				let res = true;
				for (let i = 0; i < this.activeMembers.length; i++) {
					if (this.activeMembers[i].type == "") {
						res = false;
						doErro.showErro("noAll");
						break;
					}
				}
				if (res) {
					uni.showLoading({
						title: "数据传输中...",
						mask: true
					})
					uniCloud.callFunction({
						name: "rc_room",
						data: {
							type: "checkRoom",
							school: this.activeSchool,
							floor: this.activeFloor,
							roomNum: this.activeRoomNum,
							roomtype: this.activeType,
							members: this.activeMembers,
							uid: this.activeUid,
							roomid:this.activeRoomId,//宿舍id
							createtime: Date.parse(new Date())
						},
						success(data) {
							if (data.result) {
								uni.hideLoading();
								uni.showModal({
									content: "提交成功"
								});
							}
						},
						fail(err) {
							console.log(err);
							doErro.showErro(500);
							uni.hideLoading();
						}
					})
				}
			},
			//修改宿舍
			setRoom() {
				//console.log("修改");
				// uni.redirectTo({
				// 	url: "/pages/create/create?roomId=" + this.activeRoomId + "&method=fix"
				// })
				uni.navigateTo({
					url: "/pages/create/create?roomId=" + this.activeRoomId + "&method=fix"
				})
			},
			//新增宿舍
			goCreateRoom() {
				// uni.redirectTo({
				// 	url: "/pages/create/create"
				// })
				uni.navigateTo({
					url: "/pages/create/create?roomId=0&method=add"
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		.choose {
			display: flex;
			flex-direction: row;
			justify-content: space-around;
			border-bottom: 0rpx solid lightgray;
			padding-bottom: 20rpx;

			picker {
				border-bottom: 1rpx solid #00aa7f;
				width: 30%;
				height: 100rpx;
				font-size: 38rpx;
				line-height: 100rpx;
				text-align: center;
			}
		}

		.add {
			position: relative;
			.addRoom {
				position: absolute;
				top: 550rpx;
				left: 600rpx;
				color: #00aa7f;
				font-size: 80rpx;
				border: 0rpx solid #00aa7f;
				border-radius: 50%;
			}
		}

		.allRoom {
			border: 0rpx solid red;
			padding-left: 5rpx;
			padding-right: 5rpx;
			margin-top: 20rpx;

			.oneRoom {
				height: 120rpx;
				line-height: 120rpx;
				border: 1rpx solid #00aa7f;
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				margin-top: 20rpx;
				font-size: 38rpx;

				.num {
					background-color: #00aa7f;
					padding-left: 20rpx;
					padding-right: 20rpx;
				}

				.info {
					padding-right: 20rpx;
					color: #00aa7f;
				}
			}
		}

		.tips {
			height: 600rpx;
			box-sizing: border-box;
			padding-top: 450rpx;
			font-size: 40rpx;
			color: lightgray;
			text-align: center;
		}

		// 以下是详情页的样式
		.roomList {
			font-size: 38rpx;

			.showRoomNum {
				height: 100rpx;
				line-height: 100rpx;
				text-align: center;
				background-color: lightgray;
				position: relative;

				.iconfont {
					height: 100rpx;
					line-height: 100rpx;
					font-size: 40rpx;
					color: black;
					position: absolute;
					left: 680rpx;
				}
			}

			.roomitem {
				.members {
					height: 100rpx;
					line-height: 100rpx;
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					border-bottom: 2rpx solid lightgray;

					.stname {
						width: 150rpx;
						text-align: center;
						background-color: #00aa7f;
						color: white;
					}

					.typelist {
						box-sizing: border-box;
						padding-right: 80rpx;

						label {
							margin-left: 20rpx;

							.zai {
								color: #00aa7f;
							}

							.li {
								color: red;
							}

							.qing {
								color: black
							}
						}
					}
				}
			}

			.btn {
				button {
					height: 100rpx;
					line-height: 100rpx;
				}
			}
		}
	}
</style>